﻿@{
    ViewBag.Title = "";
    Layout = "~/Views/Shared/_Form.cshtml";
 }

<form id="form" class="form-horizontal m" role="form">
    <div class="form-group">
        <label class="col-sm-3 control-label ">渠道名称<span class="text-red"> *</span></label>
        <div class="col-sm-9">
            <input id="name" name="Name" col="Name" type="text" class="form-control required" placeholder="请输入渠道名称" />
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label ">渠道地址<span class="text-red"> *</span></label>
        <div class="col-sm-3">
            <select id="provinceId" col="ProvinceId" name="ProvinceId" class="form-control select2 required">
                <option value="">请选择省</option>
            </select>
        </div>
        <div class="col-sm-3">
            <select id="cityId" col="CityId" name="CityId" class="form-control select2 required">
                <option value="">请选择市</option>
            </select>
        </div>
        <div class="col-sm-3">
            <select id="areaId" col="AreaId" name="AreaId" class="form-control select2 required">
                <option value="">请选择区</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label ">渠道经理<span class="text-red"> *</span></label>
        <div class="col-sm-9">
            <input id="mangerName" name="MangerName" col="MangerName" type="text" class="form-control required" placeholder="请输入渠道经理姓名" />
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label ">联系方式<span class="text-red"> *</span></label>
        <div class="col-sm-9">
            <input id="mangerPhone" name="MangerPhone" col="MangerPhone" type="text" class="form-control required" placeholder="请输入联系方式" />
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label ">详细地址<span class="text-red"> *</span></label>
        <div class="col-sm-9">
            <textarea id="address" name="Address" col="Address" type="text" class="form-control required" rows="5" placeholder="请输入详细地址"></textarea> 
        </div>
    </div>
</form>

<script type="text/javascript">
    var id = frame.request("id");
    var managerId = frame.request("managerId");
    $(function () { 
        renderProvinceId();
        getForm();
    }); 

    function renderProvinceId() {
        $("#provinceId").frameSelect({
            url: '@Url.Action("GetProvince", "Common", new { area = "" })',
            search: true,
            onChange: function (event) {
                $("#cityId option:not(:first)").remove();
                $("#areaId option:not(:first)").remove();
                if (!!event.target.value) {
                    renderCityId(event.target.value);
                    $(this).valid();
                }

            }
        });
    }

    function renderCityId(provinceId) {
        $("#cityId").frameSelect({
            url: '@Url.Action("GetCity", "Common", new { area = "" })',
            param: { provinceId },
            search: true,
            onChange: function (event) {
                $("#areaId option:not(:first)").remove();
                if (!!event.target.value) {
                    renderAreaId(event.target.value);
                    $(this).valid();
                }
            }
        });
    }

    function renderAreaId(cityId) {
        $("#areaId").frameSelect({
            url: '@Url.Action("GetArea", "Common", new { area = "" })',
            param: { cityId },
            search: true,
            onChange: function (event) {
                if (!!event.target.value) {
                    $(this).valid();
                }
            }
        });
    }

    function getForm() {
        if (!!id) {
            frame.ajax({
                url: '@Url.Content("/Sale/Channel/SetChanelForm")' + '?id=' + id + "&managerId=" + managerId,
                type: 'get',
                success: function (obj) {
                    if (obj.success) {
                        $('#form').setWebControls(obj.data);
                    }
                }
            });
        }
        else {
            var defaultData = {};
            $('#form').setWebControls(defaultData);
        }
    }

    function saveForm(index) {
        if ($('#form').validate().form()) {
            var postData = $('#form').getWebControls({ Id: id, ManagerId: managerId }); 
            frame.ajax({
                url: '@Url.Content("/Sale/Channel/SaveForm")',
                type: 'post',
                data: postData,
                success: function (obj) {
                    if (obj.success) {
                        frame.msgSuccess(obj.message);
                        parent.searchGrid();
                        parent.layer.close(index);
                    }
                    else {
                        frame.msgError(obj.message);
                    }
                }
            });
        }
    }
</script>

